﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PreeRegisterModel;
using System.Data;
using PreeRegisterDAL;

namespace PreeRegisterBLL
{
    public class IdentityGetBLL
    {
        public static IdentityGetResponseModel IdentityGet(IdentityGetRequestModel identityGetRequestModel)
        {
            try
            {                
                IdentityGetResponseModel identityGetResponseModel = new PreeRegisterModel.IdentityGetResponseModel();

                //校验入口参数  liujia160104
                int code = 0;
                string msg = "";
                VerifyInput(identityGetRequestModel, ref msg, ref code);
                if (code == 0)
                {
                    identityGetResponseModel.code = code.ToString();
                    identityGetResponseModel.msg = msg;
                    return identityGetResponseModel;
                }

                //根据出生日期获取年龄和年龄单位  liujia160104
                int age = 0, ageunit = 1;
                DrSoftsFrame.Common.SetAge(DrSoftsFrame.Common.ToDateTime(identityGetRequestModel.Birthday), ref age, ref ageunit);
                identityGetRequestModel.age = age.ToString();
                identityGetRequestModel.ageunit = ageunit.ToString();

                //查询诊疗ID信息
                //DataSet ds = IdentityGetDAL.IdentityGet(identityGetRequestModel);
                DataSet dsGet = new DataSet();
                if (VariableBLL.IFBindZLK)  //绑定诊疗卡模式
                {
                    //诊疗卡模式下，从诊疗卡表查询在用诊疗卡信息  liujia151125
                    dsGet = IdentityGetDAL.GetZLKCardCode(identityGetRequestModel);
                }
                else
                {
                    dsGet = IdentityGetDAL.GetPatientCode(identityGetRequestModel);
                }

                //查询到诊疗ID信息
                if (dsGet != null && dsGet.Tables[0].Rows.Count > 0)   
                {
                    //赋值
                    identityGetResponseModel.code = "1";
                    identityGetResponseModel.msg = "获取诊疗ID号成功！";

                    IdentityGetResponseResultModel result = new IdentityGetResponseResultModel();
                    result.Address = dsGet.Tables[0].Rows[0]["cAddress"].ToString();
                    result.age = dsGet.Tables[0].Rows[0]["iYearsOld"].ToString();
                    result.ageunit = dsGet.Tables[0].Rows[0]["iYearsUnit"].ToString();
                    result.Birthday = dsGet.Tables[0].Rows[0]["dBirthday"].ToString();
                    result.identityCard = dsGet.Tables[0].Rows[0]["cIdentityCard"].ToString();
                    result.PatientName = dsGet.Tables[0].Rows[0]["cPatientName"].ToString().Trim();
                    result.Sex = dsGet.Tables[0].Rows[0]["cSex"].ToString();
                    result.tel = dsGet.Tables[0].Rows[0]["cRelationType"].ToString();
                    result.PatientCode = dsGet.Tables[0].Rows[0]["cPatientCode"].ToString().Trim();
                    //增加诊疗卡身编号查询   liujia151203
                    if (dsGet.Tables[0].Rows[0]["cCardNum"].ToString().Trim() == "")
                    {
                        result.zlkCardNum = "null";
                    }
                    else
                    {
                        result.zlkCardNum = dsGet.Tables[0].Rows[0]["cCardNum"].ToString().Trim();
                    }
                    
                    identityGetResponseModel.result = result;

                    //返回
                    return identityGetResponseModel;
                }
                else   //未查询到诊疗ID信息
                {

                    //生成患者就诊号  
                    string cPatientCode = "";
                    string ZLKFlowCode = "";

                    if (VariableBLL.IFBindZLK)  //绑定诊疗卡模式
                    {
                        ZLKFlowCode = IdentityGetDAL.CreateZLKFlowCode(identityGetRequestModel.userid);

                        if (ZLKFlowCode == "")
                        {
                            identityGetResponseModel.code = "0";
                            identityGetResponseModel.msg = "获取诊疗ID号失败：获取诊疗卡账户流水号失败！";
                            return identityGetResponseModel;
                        }
                        cPatientCode = VariableBLL.HosNamey.PadLeft(4, '0') + DateTime.Now.Year.ToString().Substring(2) + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + "1" + ZLKFlowCode.PadLeft(4, '0');
                    }
                    else  //非绑定诊疗卡模式
                    {
                        cPatientCode = IdentityGetDAL.CreateNewPatientCode().ToString();
                    }


                    //生成患者ID
                    int iPatientID = 0;
                    int ReturnValue=-1000;
                    string ls_error = "";
                    iPatientID = IdentityGetDAL.CreatePatientid(ref ReturnValue, ref ls_error);
                    if (ReturnValue != 1)
                    {
                        identityGetResponseModel.code = "0";
                        identityGetResponseModel.msg = "获取诊疗ID号失败：生成患者ID失败！";
                        return identityGetResponseModel;
                    }


                    //插入HIS患者表
                    DataSet dsInsert = IdentityGetDAL.InsertHisOpdpatient(identityGetRequestModel, cPatientCode, iPatientID);
                    if (dsInsert == null || dsInsert.Tables.Count == 0 || dsInsert.Tables[0].Rows.Count == 0)
                    {
                        identityGetResponseModel.code = "0";
                        identityGetResponseModel.msg = "获取诊疗ID号失败：插入HIS患者记录失败！";
                        return identityGetResponseModel;
                    }
                    string patientcode = dsInsert.Tables[0].Rows[0]["code"].ToString();
                    string patientmsg = dsInsert.Tables[0].Rows[0]["msg"].ToString();
                    if (patientcode == "0")
                    {
                        identityGetResponseModel.code = "0";
                        identityGetResponseModel.msg = "获取诊疗ID号失败：" + patientmsg;
                        return identityGetResponseModel;
                    }


                    //写入诊疗卡账户
                    if (VariableBLL.IFBindZLK)  
                    {
                        DataSet dsZLK = IdentityGetDAL.CreateZLKCard(identityGetRequestModel, cPatientCode, iPatientID);
                        if (dsZLK == null || dsZLK.Tables.Count < 1 || dsZLK.Tables[0].Rows.Count < 1)
                        {
                            IdentityGetDAL.DelHISPatient(cPatientCode);

                            identityGetResponseModel.code = "0";
                            identityGetResponseModel.msg = "获取诊疗ID号失败：创建ZLK账户失败！";
                            return identityGetResponseModel;
                        }
                        string Result = dsZLK.Tables[0].Rows[0]["Result"].ToString();
                        string Msg = dsZLK.Tables[0].Rows[0]["Msg"].ToString();
                        if (Result == "1")
                        {
                            IdentityGetDAL.DelHISPatient(cPatientCode);

                            identityGetResponseModel.code = "0";
                            identityGetResponseModel.msg = "获取诊疗ID号失败：创建ZLK账户失败：" + Msg;
                            return identityGetResponseModel;
                        }

                    }


                    //赋值
                    identityGetResponseModel.code = "1";
                    identityGetResponseModel.msg = "获取诊疗ID号成功！";

                    IdentityGetResponseResultModel result = new IdentityGetResponseResultModel();
                    result.Address = dsInsert.Tables[0].Rows[0]["cAddress"].ToString();
                    result.age = dsInsert.Tables[0].Rows[0]["iYearsOld"].ToString();
                    result.ageunit = dsInsert.Tables[0].Rows[0]["iYearsUnit"].ToString();
                    result.Birthday = dsInsert.Tables[0].Rows[0]["dBirthday"].ToString();
                    result.identityCard = dsInsert.Tables[0].Rows[0]["cIdentityCard"].ToString();
                    result.PatientName = dsInsert.Tables[0].Rows[0]["cPatientName"].ToString().Trim();
                    result.Sex = dsInsert.Tables[0].Rows[0]["cSex"].ToString();
                    result.tel = dsInsert.Tables[0].Rows[0]["cRelationType"].ToString();
                    result.PatientCode = dsInsert.Tables[0].Rows[0]["cPatientCode"].ToString().Trim();
                    //增加诊疗卡身编号查询   liujia15120
                    result.zlkCardNum = "";
                    identityGetResponseModel.result = result;


                }

                //返回
                return identityGetResponseModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }

        /// <summary>
        /// 校验数据合法性  liujia160104
        /// </summary>
        /// <param name="identityGetRequestModel"></param>
        /// <param name="strRef"></param>
        /// <param name="ReturnValue"></param>
        private static void VerifyInput(IdentityGetRequestModel identityGetRequestModel, ref string strRef, ref int ReturnValue)
        {
            try
            {
                //校验身份证号是否为空  liujia151203
                if (identityGetRequestModel.identityCard.Trim() == "")
                {
                    ReturnValue = 0;
                    strRef = "身份证号校验失败：身份证号为空！";
                    return;
                }
                if (identityGetRequestModel.identityCard.Trim() == "null")
                {
                    ReturnValue = 0;
                    strRef = "身份证号校验失败：身份证号为null！";
                    return;
                }

                //校验患者姓名是否为空  liujia151218
                if (identityGetRequestModel.PatientName.Trim() == "")
                {
                    ReturnValue = 0;
                    strRef = "患者姓名校验失败：患者姓名为空！";
                    return;
                }
                if (identityGetRequestModel.PatientName.Trim() == "null")
                {
                    ReturnValue = 0;
                    strRef = "患者姓名校验失败：患者姓名为null！";
                    return;
                }

                //性别  
                if (identityGetRequestModel.Sex.Trim() == "")
                {
                    ReturnValue = 0;
                    strRef = "患者性别校验失败：患者性别为空！";
                    return;
                }
                if (identityGetRequestModel.Sex.Trim() == "null")
                {
                    ReturnValue = 0;
                    strRef = "患者性别校验失败：患者性别为null！";
                    return;
                }

                //出生日期  
                if (identityGetRequestModel.Birthday.Trim() == "")
                {
                    ReturnValue = 0;
                    strRef = "患者性别校验失败：患者出生日期为空！";
                    return;
                }
                if (identityGetRequestModel.Birthday.Trim() == "null")
                {
                    ReturnValue = 0;
                    strRef = "患者性别校验失败：患者出生日期为null！";
                    return;
                }

                //操作员编码 
                if (identityGetRequestModel.userid.Trim() == "")
                {
                    ReturnValue = 0;
                    strRef = "患者性别校验失败：患者操作员编码为空！";
                    return;
                }
                if (identityGetRequestModel.userid.Trim() == "null")
                {
                    ReturnValue = 0;
                    strRef = "患者性别校验失败：患者操作员编码为null！";
                    return;
                }

                ReturnValue = 1;
                strRef = "患者性别校验成功！";
                return;

            }
            catch (Exception err)
            {
                
                throw;
            }
        }
    }
}
